﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using Setup;

namespace SubtitlesAutoLoader
{
    public class AutoLoader
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public AutoLoader()
        { 
            
        }

        /// <summary>
        /// Methods
        /// </summary>
        /// <param name="player"></param>
        /// <param name="movie"></param>
        public void PlayMovie(string player, string movie)
        {
            string sub = this.FindSubtitles(new FileInfo(movie).Directory.FullName, Path.GetFileName(movie));

            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.WorkingDirectory = Path.GetDirectoryName(player);
            processStartInfo.FileName = player;

            processStartInfo.Arguments = "\"" + movie + "\"" + " /sub " + "\"" + sub + "\"";

            Process.Start(processStartInfo);  
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="movieName"></param>
        /// <returns></returns>
        private string FindSubtitles(string dir, string movieName)
        {
            string[] files = Directory.GetFiles(dir, "*.srt");

            if (files.Length == 0) return null;

            SubtitlesSearcher subSearcher = new SubtitlesSearcher(files);

            return subSearcher.searchBestFitSub(movieName);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class SubtitlesInfo
    {
        public string SubtitlesFileName;
        public string SubtitlesFullFileName;
        public int SimilarityWithMovieFileName;

        public SubtitlesInfo(string name)
        {
            this.SubtitlesFullFileName = name;
            this.SubtitlesFileName = Path.GetFileName(name);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class SubtitlesSearcher
    {
        private List<SubtitlesInfo> SubInfo;

        public SubtitlesSearcher(string[] subfiles)
        {
            SubInfo = new List<SubtitlesInfo>();

            foreach (string file in subfiles)
            {
                SubInfo.Add(new SubtitlesInfo(file));
            }
        }

        public string searchBestFitSub(string movieFileName)
        {
            string SubToUse = null;
            int bestSimilarity;

            foreach (SubtitlesInfo info in SubInfo)
            {
                info.SimilarityWithMovieFileName = StringComparator.Compare(movieFileName, info.SubtitlesFileName);
            }

            SubToUse = SubInfo[0].SubtitlesFullFileName;
            bestSimilarity = SubInfo[0].SimilarityWithMovieFileName;

            foreach (SubtitlesInfo info in SubInfo)
            {
                if (bestSimilarity > info.SimilarityWithMovieFileName)
                {
                    SubToUse = info.SubtitlesFullFileName;
                    bestSimilarity = info.SimilarityWithMovieFileName;
                }
            }

            return SubToUse;
        }
    }
}
